package com.tanhua.app.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.api.*;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.pojo.*;
import com.tanhua.model.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SoulService {
    @DubboReference//查询选项
    private QuestionnaireOptionApi optionApi ;

    @DubboReference//查询题目
    private QuestionnaireQuestionApi questionApi;

    @DubboReference//查询所有试卷
    private QuestionnaireApi questionnaireApi;

    @DubboReference
    private QuestionnaireResultApi resultApi;

    @DubboReference
    private DimensionsApi dimensionsApi;

    @DubboReference
    private IdsimilarYouApi idsimilarYouApi;

    //查询
    public List<QuestionnaireVo> getQuestionnaireVo() {
        //查询所有的试卷
        List<Questionnaire> questions = questionnaireApi.selectAll();
        //把查询出来的数据封装
        //一个集合
        List<QuestionnaireVo> vos = new ArrayList<>();

        for (Questionnaire question : questions) {
            QuestionnaireVo vo = new QuestionnaireVo();

            vo.setName(question.getName());
            vo.setId(question.getId()+"");
            vo.setCover(question.getCover());
            vo.setLevel(question.getLevel());
            vo.setStar(question.getStar());
            vo.setIsLock(0);
            vo.setReportId(null);

            List<QuestionnaireQuestionVo> questionVos = new ArrayList<>();

            //更具问卷id来条件查询
            List<QuestionnaireQuestion> questionlist = questionApi.selectByIdList(question.getId());
            //遍历得到的问题
            for (QuestionnaireQuestion questionnaireQuestion : questionlist) {
                //创建封装对象
                QuestionnaireQuestionVo questionVoo = new QuestionnaireQuestionVo();

                List<QuestionnaireOptionVo> options = new ArrayList<>();

                //把问题id和问题名称放入到对象中
                questionVoo.setId(questionnaireQuestion.getId()+"");
                questionVoo.setQuestion(questionnaireQuestion.getQuestion());

                //根据问题id来查询所匹配的选项
                List<QuestionnaireOption> optionList = optionApi.selectByIdList(questionnaireQuestion.getId());

                //循环这个对象
                for (QuestionnaireOption questionnaireOption : optionList) {
                    QuestionnaireOptionVo optionVo = new QuestionnaireOptionVo();
                    optionVo.setId(questionnaireOption.getId()+"");
                    optionVo.setOption(questionnaireOption.getChoice());
                    //把选项数据存入到对象中
                    options.add(optionVo);
                }
                //在把options存到对象中
                questionVoo.setOptions(options);
                //questionVo的对象存入到集合中
                questionVos.add(questionVoo);
            }
            vo.setQuestions(questionVos);
            vos.add(vo);

        }
        return  vos;
    }



    //查询对象的分数
    public Integer findSelectByQuestionId(long id, long questionId) {
        //创建查询条件
        Integer number = optionApi.selectByQuestionId(id,questionId);
        return  number;
    }

    //把结果存储起来
    public Long save(QuestionnaireResult result) {
        Long id = resultApi.save(result);
        return id;
    }

    public QuestionnaireResult select(List<Answers> answersList) {
        //定义一个用于计算总分的变量
        Integer sum = 0;
        //遍历集合，得到每一个问题编号和选项编号的对象
        for (Answers answer : answersList) {
            String questionId = answer.getQuestionId();
            String optionId = answer.getOptionId();
            Integer Number = optionApi.selectByQuestionId(Integer.parseInt(questionId),Integer.parseInt(optionId));
            sum += Number;
        }

        //把相应的数据存入到结果表，并返回id
        QuestionnaireResult result = new QuestionnaireResult();
        result.setUserId(UserHolder.getUserId());
        if (sum>=10 && sum <= 20){
            //狐狸
            result.setConclusion(Constants.CHARACTER_FOX);
            result.setCover(Constants.CHARACTER_FOX);
            result.setType("老狐狸，拥有丰富的人生经历");
        }else if (sum >= 21 && sum <= 40) {
            //狮子型
            result.setConclusion(Constants.CHARACTER_LION);
            result.setCover(Constants.COVER_LION);
            result.setType("狮子，自信心十足 ，做事果断");
        }else if (sum >= 41 && sum <= 60) {
            //白兔
            result.setConclusion(Constants.CHARACTER_RABBIT);
            result.setCover(Constants.COVER_RABBIT);
            result.setType("白兔，生活中的小白，没有什么社会经历");
        }else if (sum >= 61) {
            //白兔
            result.setConclusion(Constants.CHARACTER_OWl);
            result.setCover(Constants.COVER_OWL);
        }
        return result;
    }

    //查询对应的数据
    public ResultVo selectResult(int ids) {
       QuestionnaireResult result = resultApi.selectById(ids);

       //创建封装对象
        ResultVo vo = new ResultVo();
        vo.setConclusion(result.getConclusion());
        vo.setCover(result.getCover());

        List<DimensionsVo> dVList = new ArrayList<>();
        //查询所有的维度
        List<Dimensions> dimensionList = dimensionsApi.selectAll();
        for (Dimensions dimensions : dimensionList) {
            DimensionsVo dimensionsVo = new DimensionsVo();
            dimensionsVo.setKey(dimensions.getKey());
            dimensionsVo.setValue(dimensions.getValue());

            dVList.add(dimensionsVo);
        }

        vo.setDimensions(dVList);
        //查询所有匹配的数据
        List<IdsimilarYouVo> yVlist = new ArrayList<>();

        List<IdsimilarYou> ylist = idsimilarYouApi.selectAll();

        for (IdsimilarYou idsimilarYou : ylist) {
            IdsimilarYouVo you = new IdsimilarYouVo();
            you.setAvatar(idsimilarYou.getAvatar());
            you.setId((int) idsimilarYou.getId());

            yVlist.add(you);
        }

        vo.setSimilarYou(yVlist);

        return vo;
    }
}
